package com.frt.sygang.controller;/**
 * Created by 冯瑞涛 on 2022/9/23 12:46
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frt.sygang.common.R;
import com.frt.sygang.entity.OrderDetail;
import com.frt.sygang.entity.Orders;
import com.frt.sygang.sevice.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author 冯瑞涛
 * @version 1.0
 * @date 2022/9/23 12:46
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @PostMapping("/submit")
    public R<String> submitOrder(@RequestBody Orders order, HttpServletRequest request) {
        log.info("提交的订单号"+order.toString());
        ordersService.submitOrder(order,request);
        return R.success("提交成功！");
    }

    @GetMapping("/userPage")
    public R<IPage<Orders>> getOrders(Integer page,Integer pageSize,HttpServletRequest request){
        IPage<Orders> iPage=new Page(page,pageSize);
        Long userId = (Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<Orders> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId,userId);
        ordersService.page(iPage, wrapper);
        return R.success(iPage);
    }

    @GetMapping("/page")
    public R<IPage<Orders>> pageOrders(Integer page, Integer pageSize, Long number, HttpServletRequest request, String beginTime,String endTime){
        IPage<Orders> iPage=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> wrapper=new LambdaQueryWrapper<>();
        String format="yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat formats=new SimpleDateFormat(format);
        Date beginDate= null;
        Date endDate=null;
        try {
            if(beginTime!=null&&endTime!=null){
                beginDate = formats.parse(beginTime);
                endDate=formats.parse(endTime);
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        wrapper.like(number!=null,Orders::getId,number);
        wrapper.ge(beginTime!=null,Orders::getOrderTime,beginDate);
        wrapper.le(endTime!=null,Orders::getCheckoutTime,endDate);
        ordersService.page(iPage,wrapper);
        List<Orders> records = iPage.getRecords();
        return R.success(iPage);
    }

    @PutMapping
    public R<String> putOrderStatus(@RequestBody Orders orders){
        ordersService.updateById(orders);
        return R.success("修改成功");
    }
}
